o = int(input())
s = str(o)
m = 0
k = 0 
h = 0
y = 1
if o < 10000 and o > 999:
    for _ in range(4):
        for i in range(2, int(s[h:y]) - 1):
            if int(s[h:y]) % i == 0:
                k = 1
                break
        if k == 0 and m < int(s[h:y]):
            m = int(s[h:y])
        k = 0
        h += 1
        y += 1
    h = 0 
    y = 2
    for _ in range(3):
        for i in range(2, int(s[h:y]) - 1):
            if int(s[h:y]) % i == 0:
                k = 1
                break
        if k == 0 and m < int(s[h:y]):
            m = int(s[h:y])
        k = 0
        h += 1
        y += 1
    h = 0 
    y = 3
    for _ in range(2):
        for i in range(2, int(s[h:y]) - 1):
            if int(s[h:y]) % i == 0:
                k = 1
                break
        if k == 0 and m < int(s[h:y]):
            m = int(s[h:y])
        k = 0
        h += 1
        y += 1
    h = 0 
    y = 4
    for i in range(2, int(s[h:y]) - 1):
        if int(s[h:y]) % i == 0:
            k = 1
            break
    if k == 0 and m < int(s[h:y]):
        m = int(s[h:y])
    if m != 1:
        print(m)
    else:
        print("0")
else:
    print("0")
